import { defineStore } from 'pinia'
import Api from '@/api'
import Dict from '@/Utils/dict'
import { sessionGet } from '@/Utils/storage'
export const useSupplyStore = defineStore('supply', {
  persist: {
    enabled: true,
  },
  state: () => {
    return {
      // 库存数量
      sthCountBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
      },
      // 库存金额
      sthMoneyBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
      },
      // 入库出库数量展示
      inOutSthCountBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
        x_axis_name:'',
      },
      // top10出库金额占比
      top10MoneyBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
      },
      // top10出库数量占比
      top10CountBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
      },
      // 国家库存结构
      countrySthBar: {
        title:'',
        sub_titles:[],
        suffix:'',
        info:[],
      },
      // 在制、在库、在途订单金额汇总
      moneySummary: {
        title:'',
        info:[],
      },
      //全球库存变化趋势图
      inventoryTrendReq:{
        title:'',
        name:'',
        sub_titles:[],
        info:[],
      },
      //库存数量与出库数量对比
      inventoryOutBoundReq:{
        title:'',
        name:'',
        sub_titles:[],
        info:[],
      },
      //呆滞库存
      inventoryUnSaleableReq:{
        title:'',
        sub_titles:[],
        info:[],
      },
      //区域库存周转率/区域库存周转天数
      inventoryTurnoverReq:{
        title:'',
        sub_titles:[],
        info:[],
      },
      // 全球仓库数
      supplyWarehouseNum:{
        // info: {}
      }
    }
  },

  actions: {
    clearVersion(){
      this.version = {}
    },
    getAll() {
      const supplyLevel = sessionGet('role')?.supplyLevel || 0
      if (!supplyLevel) {
        return ''
      }
      // Dict对象获取supply的所有key,然后放到一个数组中
      const keys = Object.keys(Dict.supply).filter((item: any) => {
        if (!item) {
          return false
        }
        return true
      })
      if (keys.length) {
        keys.forEach((item: any) => {
            try{
              Api[item]().then((res: any) => {
                if (res.code == 0 || res.code == 10000) {
                  if(res.data){
                    this[item] = res.data
                  }
                }
              })
            }catch(e){
              console.log('err',e)
            }
         
        })
      }

      
       
    }
  },
})
